<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>系统测试页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .status {
            padding: 10px;
            margin: 10px 0;
            border-radius: 4px;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .info {
            background-color: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }
        button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .test-result {
            margin: 10px 0;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>系统状态检查</h1>
        
        <div class="status success">
            ✅ 前端服务运行在: <a href="http://localhost:80" target="_blank">http://localhost:80</a>
        </div>
        
        <div class="status success">
            ✅ 后端服务运行在: <a href="http://localhost:8080" target="_blank">http://localhost:8080</a>
        </div>
        
        <h2>API测试</h2>
        
        <button onclick="testBackendHealth()">测试后端健康状态</button>
        <button onclick="testTaskArrangementAPI()">测试任务安排API</button>
        <button onclick="loginAndTest()" style="background-color: #007bff;">登录并测试</button>
        <button onclick="testTimerTasks()">测试定时任务</button>
        <button onclick="testTodayPending()">测试今日待执行</button>
        <button onclick="createTestData()" style="background-color: #28a745;">生成测试数据</button>
        
        <div id="testResults"></div>
        
        <h2>访问链接</h2>
        <ul>
            <li><a href="http://localhost:80" target="_blank">前端主页</a></li>
            <li><a href="http://localhost:80/#/system/arrangement" target="_blank">任务安排页面</a></li>
            <li><a href="http://localhost:8080/swagger-ui.html" target="_blank">后端API文档</a></li>
        </ul>
    </div>

    <script>
        function addResult(message, type = 'info') {
            const results = document.getElementById('testResults');
            const div = document.createElement('div');
            div.className = `test-result ${type}`;
            div.innerHTML = `<strong>${new Date().toLocaleTimeString()}</strong>: ${message}`;
            results.appendChild(div);
        }

        async function testBackendHealth() {
            try {
                const response = await fetch('http://localhost:8080/actuator/health');
                if (response.ok) {
                    const data = await response.json();
                    addResult(`后端健康检查成功: ${JSON.stringify(data)}`, 'success');
                } else {
                    addResult(`后端健康检查失败: ${response.status}`, 'error');
                }
            } catch (error) {
                addResult(`后端连接失败: ${error.message}`, 'error');
            }
        }

        async function testTaskArrangementAPI() {
            try {
                const response = await fetch('http://localhost:8080/system/arrangement/list');
                if (response.status === 401) {
                    addResult('任务安排API需要认证 (401) - 这是正常的', 'info');
                } else if (response.ok) {
                    addResult('任务安排API访问成功', 'success');
                } else {
                    addResult(`任务安排API错误: ${response.status}`, 'error');
                }
            } catch (error) {
                addResult(`任务安排API连接失败: ${error.message}`, 'error');
            }
        }

        async function testLogin() {
            try {
                // 先获取验证码
                const captchaResponse = await fetch('http://localhost:8080/captchaImage');
                if (captchaResponse.ok) {
                    addResult('验证码获取成功', 'success');
                } else {
                    addResult('验证码获取失败', 'error');
                }
            } catch (error) {
                addResult(`登录测试失败: ${error.message}`, 'error');
            }
        }

        async function loginAndTest() {
            try {
                addResult('开始登录流程...', 'info');
                
                // 1. 获取验证码
                const captchaResponse = await fetch('http://localhost:8080/captchaImage');
                if (!captchaResponse.ok) {
                    addResult('获取验证码失败', 'error');
                    return;
                }
                const captchaData = await captchaResponse.json();
                addResult('验证码获取成功', 'success');
                
                // 2. 执行登录
                const loginResponse = await fetch('http://localhost:8080/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        username: 'admin',
                        password: 'admin123',
                        code: captchaData.uuid,
                        uuid: captchaData.uuid
                    })
                });
                
                if (loginResponse.ok) {
                    const loginData = await loginResponse.json();
                    if (loginData.token) {
                        localStorage.setItem('token', loginData.token);
                        addResult('登录成功！', 'success');
                        
                        // 3. 测试需要认证的API
                        await testAuthenticatedAPIs();
                    } else {
                        addResult('登录失败：' + loginData.msg, 'error');
                    }
                } else {
                    addResult('登录请求失败：' + loginResponse.status, 'error');
                }
            } catch (error) {
                addResult(`登录流程失败: ${error.message}`, 'error');
            }
        }

        async function testAuthenticatedAPIs() {
            try {
                // 测试定时任务
                const timerResponse = await fetch('http://localhost:8080/system/arrangement/generate-test-tasks', {
                    method: 'POST',
                    headers: {
                        'Authorization': 'Bearer ' + localStorage.getItem('token')
                    }
                });
                if (timerResponse.ok) {
                    const data = await timerResponse.json();
                    addResult(`定时任务测试成功: ${data.msg}`, 'success');
                } else {
                    addResult(`定时任务测试失败: ${timerResponse.status}`, 'error');
                }
                
                // 测试今日待执行
                const todayResponse = await fetch('http://localhost:8080/system/arrangement/pc/today-pending?pageNum=1&pageSize=10', {
                    headers: {
                        'Authorization': 'Bearer ' + localStorage.getItem('token')
                    }
                });
                if (todayResponse.ok) {
                    const data = await todayResponse.json();
                    addResult(`今日待执行测试成功: 共${data.total}条记录`, 'success');
                } else {
                    addResult(`今日待执行测试失败: ${todayResponse.status}`, 'error');
                }
            } catch (error) {
                addResult(`认证API测试失败: ${error.message}`, 'error');
            }
        }

        async function testTimerTasks() {
            try {
                const response = await fetch('http://localhost:8080/system/arrangement/generate-test-tasks', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': 'Bearer ' + (localStorage.getItem('token') || '')
                    }
                });
                if (response.ok) {
                    const data = await response.json();
                    addResult(`定时任务测试: ${data.msg}`, 'success');
                } else {
                    addResult(`定时任务测试失败: ${response.status}`, 'error');
                }
            } catch (error) {
                addResult(`定时任务测试连接失败: ${error.message}`, 'error');
            }
        }

        async function testTodayPending() {
            try {
                const response = await fetch('http://localhost:8080/system/arrangement/pc/today-pending?pageNum=1&pageSize=10', {
                    headers: {
                        'Authorization': 'Bearer ' + (localStorage.getItem('token') || '')
                    }
                });
                if (response.ok) {
                    const data = await response.json();
                    addResult(`今日待执行测试成功: 共${data.total}条记录`, 'success');
                } else if (response.status === 401) {
                    addResult('今日待执行测试需要认证 (401) - 这是正常的', 'info');
                } else {
                    addResult(`今日待执行测试失败: ${response.status}`, 'error');
                }
            } catch (error) {
                addResult(`今日待执行测试连接失败: ${error.message}`, 'error');
            }
        }

        async function createTestData() {
            try {
                addResult('正在生成测试数据...', 'info');
                const response = await fetch('http://localhost:8080/system/arrangement/create-test-data', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    }
                });
                if (response.ok) {
                    const data = await response.json();
                    addResult(`✅ ${data.msg}`, 'success');
                    addResult('请刷新任务安排页面查看新数据', 'info');
                } else {
                    const errorText = await response.text();
                    addResult(`生成测试数据失败: ${response.status} - ${errorText}`, 'error');
                }
            } catch (error) {
                addResult(`生成测试数据连接失败: ${error.message}`, 'error');
            }
        }

        // 页面加载时自动测试
        window.onload = function() {
            addResult('系统测试页面已加载', 'info');
            testBackendHealth();
        };
    </script>
</body>
</html>
